import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home.vue'
import Login from '../components/Login.vue'
import Mv from '../components/user/Mv.vue'
import About from '../components/user/About.vue'
import Recommend from '../components/discover/Recommend.vue'
import Songdetaill from '../components/songs/Songdetaill.vue'
import Playbar from '../components/comm/Playbar.vue'

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/home' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
    },
    {
      path: '/mv',
      component: Mv,
    },
    {
      path: '/about',
      component: About,
    },
    {
      path: '/recommend',
      component: Recommend,
    },
    // {
    //   path: '/songdetaill/:id/:songname/:image/:singername',
    //   component: Songdetaill,
    // },
    {
      path: '/songdetaill',
      component: Songdetaill,
    },
    {
      path: '/playbar',
      component: Playbar,
    },
  ],
  mode: 'history',
})

//g挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 代表从那个路径跳转过来
  //next 是一个函数 表示放行
  // next() 放行， next('/login') 强制跳转

  //如果访问是 登录页  直接放行
  if (to.path == '/login') return next()
  //获取token
  const tokenStr = window.localStorage.getItem('token')
  // console.log(tokenStr)
  if (!tokenStr) return next('/login')
  next()
})
// const router = new VueRouter({
//   mode: "history",
//   routes,
// });

export default router
